package com.vf.editor.mvc.scene.layer;

import java.util.Date;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.Scene;
import com.vf.editor.common.model.resources.SceneLayer;
import com.vf.editor.common.util.UuidUtil;

@Path(value = "/scene/layer", viewPath = "/views/scene/layer/")
public class SceneLayerController extends BaseController{

	@Inject
	private SceneLayerService sceneLayerService;
	
	public void index() {
		Scene scene=Scene.dao.findById(getPara("sceneId"));
		this.set("scene", scene);
		render("index.html");
	}
	
	public void save() {
		SceneLayer model = getModel(SceneLayer.class);
		if (StrKit.notBlank(model.getId())) {
			if (model.update()) {
				if(StrKit.equals("1", model.getShow())) {
					SceneLayer thing= SceneLayer.dao.findById(model.getId());
					if(thing==null) {
						renderSuccess("更新成功！",model);
						return;
					}
					thing=sceneLayerService.getThing(thing);
					if(thing==null) {
						renderSuccess("更新成功！",model);
						return;
					}
					renderSuccess("更新成功！",thing);
					return;
				}
				renderSuccess("更新成功！",model);
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			model.setCreateTime(new Date(System.currentTimeMillis()));
			if (model.save()) {
				renderSuccess("保存成功！", model);
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void tree() {
		render("tree.html");
	}
	
	public void getTree() {
		String id = this.get("id", this.get("sceneId", "root"));
		String show = this.getPara("show");
		renderJson(sceneLayerService.getTree(id,show));
	}
	
	public void delete() {
		SceneLayer sceneLayer=SceneLayer.dao.findById(this.getPara("layerId"));
		if(sceneLayer!=null) {
			if(sceneLayer.delete()) {
				this.renderSuccess("删除成功！", sceneLayer);
				sceneLayerService.deletes(sceneLayer);
			}else {
				this.renderError("删除失败！");
			}
		}else {
			this.renderError("数据不存在！");
		}
	}
	
	public void getById() {
		SceneLayer sceneLayer=SceneLayer.dao.findById(this.getPara("id"));
		if(sceneLayer!=null) {
			renderSuccess("查询成功！", sceneLayer);
		}else {
			renderError("数据不存在！");
		}
	}
	
	public void getThing() {
		SceneLayer sceneLayer=SceneLayer.dao.findById(this.getPara("id"));
		if(sceneLayer!=null) {
			sceneLayer =sceneLayerService.getThing(sceneLayer);
			if(sceneLayer!=null) {
				renderSuccess("成功！", sceneLayer);
			}else {
				renderError("数据不存在！");
			}
		}else {
			renderError("数据不存在！");
		}
	}
	
	public void getChildren() {
		String code=this.getPara("code");
		String isExpand=this.getPara("isExpand");
		SceneLayer  sceneLayer=SceneLayer.dao.findFirst("select * from "+SceneLayer.TABLE_NAME+" where code=? ",code);
		if(sceneLayer!=null) {
			this.renderSuccess("成功！", sceneLayerService.getTree(sceneLayer.getId(),isExpand));
		}
		else {
			renderError("无效图层code！");
		}
	}
	
	public void getThings() {
		String id=this.getPara("id");
		SceneLayer  sceneLayer=SceneLayer.dao.findById(id);
		if(sceneLayer!=null) {
			this.renderSuccess("成功！", sceneLayerService.getThings(sceneLayer.getId()));
		}
		else {
			renderError("数组不存在！");
		}
	}
	
}
